import rehypeKatex from 'rehype-katex'
import rehypeStringify from 'rehype-stringify'
import remarkMath from 'remark-math'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'


enum Mode {
    code = 'code',
    latex = 'latex',
}
enum Msg_id {
    user = 'user',
    bot = 'bot',
}
enum Llm_id {
    yiyan = 'yiyan',
    doubao = 'doubao',
    spark = 'spark',
    glm = 'glm',
    qwen = 'qwen',
    gemini = 'gemini',
}
enum LlmMapList {
    yiyan = 'YiyanList',
    doubao = 'DuobaoList',
    spark = 'SparkList',
    glm = 'GlmList',
    qwen = 'QwenList',
    gemini = 'GeminiList'
}
enum ProcessLocate {
    local,
    server
}
enum State {
    loading,
    working
}
interface Msg {
    id: Msg_id
    content: string
    orign_msg: string
}
interface Status {
    mode: Mode,
    llm_id: Llm_id,
    state: State
}
interface MesgList {
    msgs: Msg[]
}
interface Operate {
    msg: string,
    processLocate: ProcessLocate,
    fuc: Function
}
function changeList(llm_id: Llm_id, callback: (llm_id: Llm_id) => void) {
    callback(llm_id)
}
function changeIcon(llm_id: Llm_id, callback: (llm_id: Llm_id) => void) {
    callback(llm_id)
}
function changeLLM(llm_id: Llm_id, callback: (llm_id: Llm_id) => void) {
    callback(llm_id)
}
async function markdownToHtml(markdown: string): Promise<string> {
    // 使用unified库将Markdown转换为HTML
    console.log('markdown is !!!', markdown);

    const file = await unified()
        .use(remarkParse)
        .use(remarkMath)
        .use(remarkRehype)
        .use(rehypeKatex)
        .use(rehypeStringify)
        .process(convertMarkdownMathToKatex(markdown))
    return file.value.toString()
}
function convertMarkdownMathToKatex(markdown: string): string {
    // // 使用正则表达式查找Markdown中的\( ... \)格式
    const inlineRegex = /\\\(.*?\\\)/gs;
    const blockRegex = /\\\[.*?\\\]/gs;
    markdown = markdown.replace(inlineRegex, (match) => `$${match.slice(2, -2)}$`);
    markdown = markdown.replace(blockRegex, (match) => `$$${match.slice(2, -2)}$$`);
   
    return markdown;
}
function sleep(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
export {
    Mode,
    Msg_id,
    Llm_id,
    LlmMapList,
    ProcessLocate,
    State,
    changeList,
    changeIcon,
    changeLLM,
    markdownToHtml,
    sleep
}
export type { Status, Msg, MesgList, Operate }

